package com.example.administrator.familfinalcialmanagement;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.administrator.familfinalcialmanagement.bean.UserInfoBean;
import com.example.administrator.familfinalcialmanagement.databinding.ActivityMainBinding;
import com.example.administrator.familfinalcialmanagement.ui.BaseActivity;
import com.example.administrator.familfinalcialmanagement.ui.RegisterActivity;
import com.example.administrator.familfinalcialmanagement.ui.activity.ForgetPwdActivity;
import com.example.administrator.familfinalcialmanagement.util.PermissionUtil;
import com.example.administrator.familfinalcialmanagement.util.SpUtils;
import com.example.administrator.familfinalcialmanagement.util.ToastUtil;

import static com.example.administrator.familfinalcialmanagement.Constants.LOGINPRESSION;
import static com.example.administrator.familfinalcialmanagement.Constants.REQEEST_CDOE_GPS;

public class MainActivity extends BaseActivity {

    ActivityMainBinding binding;

    UserInfoBean userInfoBean = (UserInfoBean) SpUtils.getBean( "user_info_bean" );

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        binding.setActivity(this);
        initView();
    }

    private void initView() {
        String phone = getIntent().getStringExtra( "module_phone" );
        String pwd = getIntent().getStringExtra( "module_pwd" );

        if (!TextUtils.isEmpty( phone )) {
            binding.phone.setText( phone );
        }

        if (!TextUtils.isEmpty( pwd )) {
            binding.pwd.setText( pwd );
        }

        String isLogin = (String) SpUtils.get("isLogin","");
        if ("1".equals(isLogin)) {
            startActivity(new Intent(MainActivity.this,GRActivity.class));
            finish();
        }

        //动态申请权限
        initPermission();
        binding.textViewRegister.setOnClickListener(this);
        binding.textViewForget.setOnClickListener(this);
        binding.textViewMain.setOnClickListener(this);
    }

    private void initPermission() {
        if (PermissionUtil.loginPermission(this, LOGINPRESSION)) {
            if (!PermissionUtil.isOPen(MainActivity.this)) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivityForResult(intent, REQEEST_CDOE_GPS);
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.textViewRegister:
                startActivity(new Intent(MainActivity.this, RegisterActivity.class));
                break;
            case R.id.textViewForget:
                startActivity(new Intent(MainActivity.this,ForgetPwdActivity.class));
                break;
            case R.id.textViewMain:

                if (userInfoBean == null) {
                    ToastUtil.showToast( "请先注册" );
                    return;
                }

                String PhoneCode = binding.phone.getText().toString().trim();
                if (TextUtils.isEmpty(PhoneCode)) {
                    ToastUtil.showToast( "请输入账号" );
                    return;
                }

                String pwd = binding.pwd.getText().toString().trim();
                if (TextUtils.isEmpty(pwd)) {
                    ToastUtil.showToast( "请输入密码" );
                    return;
                }

                if (!PhoneCode.equals( userInfoBean.getName() ) || !pwd.equals( userInfoBean.getPwd() )) {
                    ToastUtil.showToast( "账号或密码有误" );
                    return;
                }

                UserInfoBean bean = new UserInfoBean();
                bean.setName( PhoneCode );
                bean.setPwd( pwd );
                SpUtils.putBean( "user_info_bean",bean );

                //TODO  隐式跳转
                startActivity(new Intent("com.gr.gravtivity"));
                break;
            default:
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return true;
    }

}
